<?php
	include_once('../config.php');
	LinkDataBase();
	
	?>
	<script type="text/javascript">
	var year = <?php echo $_POST['year']; ?>;
	var season = '<?php echo $_POST['season']; ?>';
	var div;
	var url;
    <?php
	$index = 0;
	
	//Find sheet in droplist.
	$sql = "SELECT * FROM droplist, sheetreveal_first 
			WHERE droplist.id = droplist_id AND droplist.year = ".$_POST['year']." AND droplist.season = '".$_POST['season']."' 
			AND droplist.name = '".$_POST['type']."' AND location_index NOT LIKE 999
			ORDER BY location_index";
	$result= mysql_query($sql);
	
	while($row = mysql_fetch_assoc($result)) {
		
		$table = $row['table_number'];
		$charttype = $row['charttype'];
		$index++;
		
		$Array_Title[] = $row['charttitle'];
	?>
		div = 'chart-<?php echo $index; ?>';
		href = 'href-<?php echo $index; ?>';
				
		ChartType = '#<?php echo $charttype; ?>';
				
		//Process keyword.
		var keyword = '';
		<?php
		if (isset($row['keyword'])) {
		?>
			keyword = '<?php echo $row['keyword']; ?>';
		<?php
		}
		?>
					
		//Process contain.
		var contain = '';
		<?php
		if (isset($row['contain'])) {
		?>
			contain = '<?php echo $row['contain']; ?>';
		<?php
		}
		?>
		
		//Process Xaxis.
		var Xaxis = '';
		<?php
		if (isset($row['x_axis'])) {
		?>
			Xaxis = '<?php echo $row['x_axis']; ?>';
		<?php
		}
		?>
		
		$("#"+div+"").load('../chart/<?php echo $charttype; ?>Chart.php',
		{'year':year, 'season':season, 'tablename': '<?php echo $table; ?>', 'keyword': keyword, 
		'contain': contain});
		
		//Change href.
		$('#content-chart td#'+href+' a').attr('href','#<?php echo $table; ?>'+ChartType+'#'+keyword+'#'+contain+'#'+Xaxis);
		<?php
	}
	
	//Process odd number.
	if ($index%2==1) {
		if ($index>6)
			$index = 6;
		else
			$index = $index-1;
	}
	?>
			
	function GotoDetail(href, title) {
					
		var year = $("#year-droplist").text();
		var season = $("#season-droplist").text();
		var type = $("#type-droplist").text();
		var Array_Href = $(href).attr('href').split('#');
		$("#content-detail").load('../chart/detail/index.php',
		{'year':year, 'season':season, 'type':type, 'tablename': Array_Href[1], 'charttype': Array_Href[2], 
		'keyword': Array_Href[3], 'contain': Array_Href[4], 'Xaxis': Array_Href[5],
		'previous_title': '<a href="#" onclick="GotoBegin();" >'+title+'</a>', 'step': 1});
		
		console.log(Array_Href);
	}
	</script>  
<div class="container2" id="content-chart">
	<div>
	<?php
	
	$tableHtml .= '<table><tr>';
	
	for ($i=1; $i<$index+1; $i++) {
		
		//Check chart counts to adjust location.
		if (($i==1)||(($i-1)%$value==0)) {
			$ClassName = 'tablegenericleft';
			
			if (($index==3)||($index>4)) {
				$value = 3;
				$CSSLocation = 'margin-left: 30px';
			}
			else {
				$value = 2;
				$CSSLocation = 'margin-left: 200px';
			}
		}
		else {
			$ClassName = 'tablegeneric';
			$CSSLocation = 'padding: 0 20px 0 20px';
		}
		
		$tableHtml .= '<td><table width="300" border="1" cellspacing="0" cellpadding="0" class="'.$ClassName.'" style="'.$CSSLocation.'"><tr><td align="center" bgcolor="#f9f9f9" color="#909090" height="40px" class="tabletop" id="href-'.($i).'">'.$Array_Title[$i-1].'<a href="#" onclick="GotoDetail(this,\''.$Array_Title[$i-1].'\');"><img src="../images/right-arrow.png" width="7" height="12" align="right" style="position:relative; top:3px; left:-10px; font-size:14pt; font-family:Helvetica, Arial, sans-serif"></a></td></tr><tr><td align="center" class="tableimg"><div id="chart-'.($i).'"></div></td></tr></table></td>';
		
		if ($i%$value==0)
			$tableHtml .= '</tr>';
	}
	
	$tableHtml .= '</table>';
	
	echo $tableHtml;
	?>
</div>
</div>